package com.atguigu.gmall.order.reciever;

import com.atguigu.gmall.order.service.OrderInfoService;
import com.atguigu.gmall.rabbit.config.MqConst;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author: atguigu
 * @create: 2023-09-18 16:00
 */
@Slf4j
@Component
public class OrderReceiver {


    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 延迟关闭订单消费者
     *
     * @param orderId 订单ID
     * @param channel
     * @param message
     */
    @SneakyThrows
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void processCloseOrder(Long orderId, Channel channel, Message message) {
        //1.业务处理 可以不处理幂等
        if (orderId != null) {
            log.info("[订单服务]监听到延迟关单消息：{}", orderId);
            orderInfoService.execCloseOrder(orderId);
        }

        //2.手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 监听订单支付成功消息；更新订单状态；发送消息通知库存服务锁定库存
     *
     * @param orderId
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY, durable = "true"),
            key = MqConst.ROUTING_PAYMENT_PAY
    ))
    public void processPaySucess(Long orderId, Message message, Channel channel) {
        //业务处理
        if (orderId != null) {
            log.info("[订单服务]监听订单支付成功消息：{}", orderId);
            orderInfoService.processPaySucess(orderId);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


    /**
     * 监听（库存服务）库存扣减结果：根据结果修改订单状态
     * 分析：当前业务不需要做幂等性处理；做事务管理
     *
     * @param stockDeductStr
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_WARE_ORDER, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_WARE_ORDER, durable = "true"),
            key = MqConst.ROUTING_WARE_ORDER
    ))
    public void processStockDeductResult(String stockDeductStr, Message message, Channel channel) {
        //业务处理
        if (StringUtils.isNotBlank(stockDeductStr)) {
            log.info("[订单服务]监听扣减库存结果：{}", stockDeductStr);
            orderInfoService.processStockDeductResult(stockDeductStr);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
